package com.tnl.app.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.Order;
import com.tnl.base.BaseDAO;
import com.tnl.utils.DateUtil;


@Repository
public class OrderDAO {
	private static final String SQL_GET_ID = "select * from tb_order  where id = ?";
	private static final String SQL_GET_ORDER_OPNID = "select * from tb_order  where openid = ? order by id desc";
	private static final String SQL_GET_ORDERCODE = "select * from tb_order  where ordercode = ?";
	private static final String SQL_GET_ALL = "select * from tb_order ";
	private static final String SQL_SAVE="INSERT INTO tb_order(openid, ordercode,employeeId,hunterId,ordescId,transactionId,orderMoney,status,orderTime,createTime,updateTime) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
	private static final String SQL_GET_ORDERS_STATUS="select * from tb_order where openid=? and (status=7 or status=?) order by id desc";
	private static final String SQL_GET_ORDERS_COMPSTATUS="select * from tb_order where openid=? and (status=4 or status=5 or status=?) order by id desc ";
	private static final String SQL_UPDATE = "update tb_order set status=?,updateTime=? where id=? order by id desc";
	private static final String SQL_UPDATE_PAYSTATUS = "update tb_order set payStatus=? where id=? order by id desc";
	private static final String SQL_UPDATE_TRANS= "insert into tb_order(openid,ordercode,ordescId,transactionId,createTime,updateTime,payMoney) values(?,?,?,?,?,?,?)";
	public List<Order> getALL() {

		return BaseDAO.queryForList(Order.class, SQL_GET_ALL);

	}
	
	public int save(Order order) {
		if (order == null) {
			return -1;
		}
		Long result =BaseDAO.save(SQL_SAVE, order.getOpenid(), order.getOrdercode(),order.getEmployeeId(),order.getHunterId(),
				order.getOrdescId(),order.getTransactionId(),order.getOrderMoney(),order.getStatus(),order.getOrderTime(),new Date(),new Date());
		if (result != null) {
			return result.intValue();
		}
		return -1;
	}
	
	public Order getById(int Id) {
		return  BaseDAO.queryForObject(Order.class, SQL_GET_ID, Id);
	}
	
	
	public List<Order> getOrderByOpenId(String openid){
		return  BaseDAO.queryForList(Order.class, SQL_GET_ORDER_OPNID, openid);
	}
	
	public List<Order> getOrdersByStatus(String openid,int status){
		return BaseDAO.queryForList(Order.class, SQL_GET_ORDERS_STATUS,openid,status);
	}
	public List<Order> getOrdersByCompStatus(String openid,int status){
		return BaseDAO.queryForList(Order.class, SQL_GET_ORDERS_COMPSTATUS,openid,status);
	}
	
	public int updateStatus(int status,int orderid) {
		return BaseDAO.execute(SQL_UPDATE, status,DateUtil.currentTime(),orderid);
	}
	
	public int updatePaystatus(int status,int orderid) {
		return BaseDAO.execute(SQL_UPDATE_PAYSTATUS, status,orderid);
	}
	//openid,ordercode,ordescId,transactionId,orderTime,createTime,updateTime,payMoney
	public int updateTransactionId(Order order) {
		if (order == null) {
			return -1;
		}
		Long result =BaseDAO.save(SQL_UPDATE_TRANS,  order.getOpenid(),order.getOrdercode(),order.getOrdescId(),
				order.getTransactionId(),DateUtil.currentTime(),DateUtil.currentTime(),order.getPayMoney());
		if (result != null) {
			return result.intValue();
		}
		return -1;
	}
	
	private static final String SQL_UPDATE_DIFF="update tb_order set difftransactionId=?,payDiffStatus=1,status=3 where id=?";
	public int updateDiffStatus(String transId,Integer id){
		return BaseDAO.execute(SQL_UPDATE_DIFF, transId,id);
	}
	private static final String SQL_UPDATE_HUNTER = "update tb_order set employeeId=?,hunterId=?,orderMoney=?,status=?,orderTime=?,proType=?,payStatus=1 where ordercode=?";
	public int updateHunterInfo(int employeeId,int hunterId,int orderMoney,int status,String orderTime,String proType,String ordercode){
		return BaseDAO.execute(SQL_UPDATE_HUNTER, employeeId,hunterId,orderMoney,status,orderTime,proType, ordercode);
	}
	
	public Order getOrderByCode(String orderCode){
		return BaseDAO.queryForObject(Order.class, SQL_GET_ORDERCODE, orderCode);
	}
	

	/**
	 * and b.spec>118
	 */
	private static final String SQL_ISPAYSALARYITEM="select count(*) from tb_order a left join tb_orderdesc b on a.ordescId=b.id where a.openid=? and b.tag like '%薪酬分析%'";
	public Long isPaySalaryItem(String openid){
		return (Long)BaseDAO.queryOneRowOneCol(SQL_ISPAYSALARYITEM, openid);
	}
	//okVMY0cToHyfcFr4t--W80Ywyk5E-1542596710-5-4200000089201803148753199989-1

}
